package com.kang.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * token校验处理器
 */
@Component
public class TokenCheckFilter implements GlobalFilter, Ordered {

    //指定好放行的路径
    public static final List<String> ALLOW_URL = Arrays.asList("/login-service/loginService/doLogin","/myUrl","/loginService/doLogin");


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 一般和前端约定好，放在请求头中， authorization
     * 1,拿到请求头url
     * 2,判断放行
     * 拿到请求头
     * 拿到token
     * 校验、
     * 放行/拦截
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        String path = request.getURI().getPath();
//        if (ALLOW_URL.contains(path)) {
//            return chain.filter(exchange);
//        }
//        HttpHeaders headers = request.getHeaders();
//        List<String> authorization = headers.get("authorization");
//        if (!CollectionUtils.isEmpty(authorization)) {
//            String token = authorization.get(0);
//            if (StringUtils.hasText(token)) {
//                String realToken = token.replaceFirst("bearer ", "");
//                String value = stringRedisTemplate.opsForValue().get(realToken);
//                if (StringUtils.hasText(realToken) && StringUtils.hasText(value)) {
//                    return chain.filter(exchange);
//                }
//            }
//        }
//        //拦截
//        ServerHttpResponse response = exchange.getResponse();
//        response.getHeaders().set("content-type","application/json;charset=utf-8");
//        Map<String,Object> map = new HashMap<>();
//        map.put("code", 401);
//        map.put("msg","未授权");
//        ObjectMapper mapper = new ObjectMapper();
//        byte[] bytes = new byte[0];
//        try {
//            bytes = mapper.writeValueAsBytes(map);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        DataBuffer wrap = response.bufferFactory().wrap(bytes);
//        return response.writeWith(Mono.just(wrap));
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 2;
    }
}
